
setwd("C:/gl/Google Drive/Issue Voting measurement error/Main findings"); #set working directory 
library(foreign)
pdf(file = "Output/figure_2.pdf",width = 7.3,height =5.5,family = "Times",pointsize =11); #use to generate PDF - don't run until your figure is in good shape

d <- read.dta("Data/stability_ind_corr_tk2.dta")
d <- d[order(d$corr0),] 
top<-1.15; #top of coefficient range
bottom <-0; #bottom of coefficient range
ccr<-d$corr0 #coefficients
ser<-1/sqrt(d$N0-3) #se
par(mfrow=c(1,1),mar=c(3,0,0,0)); #number of panels and margins
case <-c("Average (precision weighted)", rev(d$issue_study));
b <- ccr;
se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;
pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr))
ser<- c(pwa_se,rev(ser))
idx<-seq(1,length(case))+.05; #+1 for Precision weighted average 

plot(c(ccr+1.64*ser,ccr-1.64*ser),c(idx+.1,idx+.1),xlim=c(bottom,top),main="",ylab="",type="n",axes=F);axis(1);
idx[1]=.25
points(ccr,idx,pch=19,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr0+1.64*ser0,idx,ccr0-1.64*ser0,idx,lwd=1);
mtext("Correlation between waves 1 and 2",side=1,line=2,cex=1);
ccr<-d$corr1 #
ser<-1/sqrt(d$N1-3) #se
b <- ccr;
se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;
pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr))
ser<- c(pwa_se,rev(ser))
points(ccr,idx,pch=1,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr+1.64*ser,idx,ccr-1.64*ser,idx,lwd=1);
text(.92,idx+0,case,cex=.6,adj = c(0,0));   
abline(h=1.1,lty=1,col="gray"); #gray line at zero
text(.20,39.75,"Incorrectly place",cex=.8,adj = c(0,0));   
text(.50,39.75,"Correctly place",cex=.8,adj = c(0,0));   

dev.off(); #saves/closes PDF - don't run unless you are generating a PDF


##Figure 3##
setwd("C:/Users/Gabe/Google Drive/Issue Voting measurement error/Main findings"); #set working directory - only necessary for saving pdfs
library(foreign)
pdf(file = "Output/figure_3.pdf",width =13,height =11,family = "Times",pointsize =11); #use to generate PDF - don't run until your figure is in good shape
par(mfrow=c(2,1),mar=c(3,2,2,0)); #number of panels and margins
top<-1.1; #top of coefficient range
bottom <--.2; #bottom of coefficient range
d <- read.dta("Data/figure_3.dta")
#Place correctly
d <- d[order(d$corr10),] 
#agree
ccr<-d$corr11 # know, agree
ser<-1/sqrt(d$N01-3) #se
case <-c("Average (precision weighted)", rev(d$issue_study));
b <- ccr;se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr));ser<- c(pwa_se,rev(ser))
idx<-seq(1,length(case))+.05; #+1 for Precision weighted average 
plot(c(ccr+1.64*ser,ccr-1.64*ser),c(idx+.1,idx+.1),xlim=c(bottom,top),main="",ylab="",type="n",axes=F);#axis(1);
title(main ="Correctly place party/candidate")
idx[1]=.35
points(ccr,idx,pch=1,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr0+1.64*ser0,idx,ccr0-1.64*ser0,idx,lwd=1);
# disagree
ccr<-d$corr10 # know, disagree
ser<-1/sqrt(d$N11-3) #se
b <- ccr;se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr));ser<- c(pwa_se,rev(ser))
points(ccr,idx,pch=19,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr+1.64*ser,idx,ccr-1.64*ser,idx,lwd=1);
text(0.97,idx+0,case,cex=.6,adj = c(0,0));   
abline(h=1.1,lty=1,col="gray"); #gray line at zero
text(.1,29.7,"Disagree in wave 1",cex=.8,adj = c(0,0));  points(.09,30,pch=19,cex=1,col=1); 
text(.650,29.7,"Agree in wave 1",cex=.8,adj = c(0,0));   points(.640,30,pch=1,cex=1,col=1);
##figure 3 - disagree
#Place incorrectly
ccr<-d$corr01 #don't know, agree
ser<-1/sqrt(d$N10-3) #se
case <-c("Average (precision weighted)", rev(d$issue_study));
b <- ccr;se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr));ser<- c(pwa_se,rev(ser))
idx<-seq(1,length(case))+.05; #+1 for Precision weighted average 
plot(c(ccr+1.64*ser,ccr-1.64*ser),c(idx+.1,idx+.1),xlim=c(bottom,top),main="",ylab="",type="n",axes=F);axis(1);
title(main ="Incorrectly placed party and candidate")
idx[1]=.35
points(ccr,idx,pch=19,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr0+1.64*ser0,idx,ccr0-1.64*ser0,idx,lwd=1);
mtext("Correlation between waves 1 and 2",side=1,line=2,cex=1);
# place correctly
ccr<-d$corr00 #don't know, disagree
ser<-1/sqrt(d$N00-3) #se
b <- ccr;se <- ser;
pwa_se <- sqrt(1/sum(1/se^2));
pwa_b <- sum(b/se^2)*pwa_se^2;pwa_b; pwa_se;
ccr<- c(pwa_b,rev(ccr));ser<- c(pwa_se,rev(ser))
points(ccr,idx,pch=1,cex=1,col=1);segments(ccr+ser,idx,ccr-ser,idx,lwd=1);#segments(ccr+1.64*ser,idx,ccr-1.64*ser,idx,lwd=1);
text(0.97,idx+0,case,cex=.6,adj = c(0,0));   
abline(h=1.1,lty=1,col="gray"); #gray line at zero
dev.off(); #saves/closes PDF - don't run unless you are generating a PDF


